const input1 = document.querySelector(".input1 input")
const input = document.querySelector(".input2 input")
const input2 = document.querySelector(".input2")
const inputs = document.querySelector(".input3 input")
const input3 = document.querySelector(".input3")
const form = document.querySelector("form")
const btn = document.querySelector("button")
const floatingTips = document.querySelector(".floatingTips")
// 手机号
const error = document.querySelector(".error")
input1.addEventListener("change", function () {
	phone()
})

// 验证码
input.addEventListener("change", function () {
	code()
})
// 输入新密码
inputs.addEventListener("change", function () {
	newPws()
	fn()
})

let btnIsDIS = true
function fn() {
	if (phone() && code() && newPws()) {
		btn.disabled = "true"
		btn.style.backgroundColor = "#EF4F3F"
		btnIsDIS = false
	} else {
		btn.disabled = "false"
		btn.style.backgroundColor = "#FADCD9"
		btnIsDIS = true
	}
}

btn.addEventListener("touchstart", function () {
	if (!btnIsDIS) {
		floatingTips.classList.add("TipsActiveClass")
		setTimeout(function () {
			floatingTips.classList.remove("TipsActiveClass")
		}, 2600)
	}
})

function phone() {
	const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
	const flag = reg.test(input1.value)
	error.style.display = flag ? "none" : "block"
	return flag
}

function code() {
	const reg = /^\d{6}$/
	const flag = reg.test(input.value)
	const error1 = input2.nextElementSibling
	error1.style.display = flag ? "none" : "block"
	return flag
}

function newPws() {
	const reg = /^\d{9}$/
	const flag = reg.test(inputs.value)

	const error1 = input3.nextElementSibling
	error1.style.display = flag ? "none" : "block"
	return flag
}

// 获取验证码
const span = document.querySelector(".input2 span")
let iscountDown = false
span.addEventListener("click", function () {
	if (iscountDown) return
	const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
	const flag = reg.test(input1.value)
	if (!flag) return
	let countDown = 3
	span.innerHTML = `${countDown}s后重新获取`
	iscountDown = true
	let setId = setInterval(function () {
		countDown--
		span.innerHTML = `${countDown}s后重新获取`

		if (countDown < 0) {
			clearInterval(setId)
			span.innerText = `获取验证码`
			iscountDown = false
			return
		}
		span.innerHTML = `${countDown}s后重新获取`
	}, 1000)
})

document
	.querySelector(".topPreset a")
	.addEventListener("touchstart", function () {
		history.back()
	})
